package com.example.demo.Config;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author WZH
 * @create 2021-10-24 18:04
 * @desc
 **/
@Slf4j
public class HBaseConfig {
    public static Configuration configuration =null;
    public static Connection connection =null;
    public static Admin admin=null;
    /*
        这里使用单例模式
    *  因为配置只需要读取一次就够了，使用static代码块，读取一次，
    * HBaseConfiguration.create() 该方法会自动读取 resources：下的hbase-site.xml文件
    * */
    static {
        try {
            configuration=HBaseConfiguration.create();
            connection = ConnectionFactory.createConnection(configuration);
            admin=connection.getAdmin();

        } catch (IOException e) {
            e.printStackTrace();
            log.warn("HBase 连接失败");
        }

    }

    public static Admin getAdmin() {
        return admin;
    }

    public static Connection getConnection() {
        return connection;
    }

    public static Configuration getConfiguration() {
        return configuration;
    }

    /*
     *   关闭资源
     * */
    public static void close(){
        if(admin!=null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(connection != null)
        {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Connection connection = HBaseConfig.getConnection();

    }
}
